package com.bgys.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bgys.common.domain.AjaxResult;
import com.bgys.common.domain.PageBean;
import com.bgys.common.utils.JsonUtil;
import com.bgys.common.utils.StringUtil;
import com.bgys.data.entity.ModelPerson;
import com.bgys.data.service.IModelPersonService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 人员模型表 前端控制器
 *
 * @author 悲歌易水
 * @since 2021-06-01
 */
@ApiModel("人员模型表")
@RestController
@RequestMapping("/data")
public class ModelPersonController {
    @Autowired
    private IModelPersonService iModelPersonService;

    @ApiOperation(value = "人员列表")
    @GetMapping("/person")
    public AjaxResult<PageBean> list(ModelPerson entity, Integer pageCurrent, Integer pageSize) {
        QueryWrapper<ModelPerson> wrapper = new QueryWrapper<>();
        if (!StringUtil.isNull(entity.getId())) {
            wrapper.eq("id", entity.getId());
        }
        if (!StringUtil.isNull(entity.getName())) {
            wrapper.like("name", entity.getName());
        }

        PageBean<ModelPerson> pageBean = null;

        if (pageCurrent != null && pageSize != null) {
            Page<ModelPerson> page = new Page<>();
            page.setCurrent(pageCurrent);
            page.setSize(pageSize);

            IPage<ModelPerson> iPage = iModelPersonService.page(page, wrapper);
            pageBean = new PageBean<>(iPage);
        } else {
            pageBean = new PageBean<>(iModelPersonService.list(wrapper));
        }
        return new AjaxResult<PageBean>(200, "查询成功", pageBean);
    }

    @ApiOperation(value = "人员信息")
    @GetMapping("/person/{id}")
    public AjaxResult<ModelPerson> info(@PathVariable("id") String id) {
        System.out.println("info " + id);
        ModelPerson person = iModelPersonService.getById(id);
        return new AjaxResult<ModelPerson>(200, "查询成功", person);
    }

    @ApiOperation(value = "人员添加")
    @PostMapping("/person")
    public AjaxResult<ModelPerson> save(@RequestBody ModelPerson entity) {
        System.out.println("name " +  entity.getName());
        JsonUtil.print(entity);
        iModelPersonService.save(entity);
        return AjaxResult.success();
    }

    @ApiOperation(value = "人员修改")
    @PatchMapping("/person")
    public AjaxResult<ModelPerson> update(@RequestBody ModelPerson entity) {
        iModelPersonService.updateById(entity);
        return AjaxResult.success();
    }

    @ApiOperation(value = "人员删除")
    @DeleteMapping("/person/{id}")
    public AjaxResult<ModelPerson> delete(@PathVariable("id") String id) {
        iModelPersonService.removeById(id);
        return AjaxResult.success();
    }
}
